package com.itheima.dao;

import com.itheima.domain.Student;

public class StudentDao implements BaseStudentDao{
    private static Student[] students = new Student[5];
    static {
        System.out.println("----------------系统启动中,数据开始初始化,请稍后!----------------");
        students[0] = new Student("001","张三","25","1999-11-11");
        students[1] = new Student("002","李四","24","1993-01-11");
        students[2] = new Student("003","王五","22","1996-12-11");
        students[3] = new Student("004","赵六","26","1997-06-11");
        System.out.println("----------------系统启动完毕,数据初始化成功,进入系统!----------------");
    }

    //添加学生的方法
    public boolean addStudent(Student student) {

        for (int i = 0; i < students.length; i++) {
            if (students[i] == null) {
                students[i] = student;
                return true;
            }
        }
        return false;
    }

    //删除学生的方法
    public void deleteStudent(String sid) {
        StudentDao studentDao = new StudentDao();
        int index = studentDao.getIndex(sid);
        students[index] = null;
    }

    //修改学生的方法
    public void updateStudent(String sid, Student student) {
        StudentDao studentDao = new StudentDao();
        int index = studentDao.getIndex(sid);
        students[index] = student;
    }

    //查看学生的方法
    public Student[] showStudent() {
        return students;
    }

    @Override
    public Student[] findAllStudent() {
        return new Student[0];
    }

    //给Service返回数组的方法
    /*public Student[] findAllStudent() {
        boolean flag = true;--------0
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null){
                flag = false;
            }
        }
        return ;
    }*/

    //根据学号查找索引的方法
    public int getIndex(String sid) {
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            if (students[i] != null) {
                if (students[i].getSid().equals(sid)) {
                    index = i;
                }
            }
        }
        return index;
    }

}
